﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com
 * Website  : http://www.wishenterprise.com
 * 
 * */

using System;
using System.Threading;
using System.Windows.Forms;
using QWEntity.Local;
using QWModel;
using QWRepository;
using Timer = System.Threading.Timer;

namespace QWControlCenter
{
    /// <summary>
    /// Main Form UI
    /// </summary>
    public partial class MainUI : Form
    {
        /// <summary>
        /// the clock widget
        /// </summary>
        private QWClock _clockWidget;

        /// <summary>
        /// timer
        /// </summary>
        private Timer _timer;

        /// <summary>
        /// logged user
        /// </summary>
        private User _user;

        /// <summary>
        /// Initializes a new instance of the <see cref="MainUI"/> class.
        /// </summary>
        public MainUI()
        {
            InitializeComponent();

            Text = @"QWine " + Application.ProductVersion;
        }

        /// <summary>
        /// Ts the clock.
        /// </summary>
        private void TClock()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(TClock);
                Invoke(o);
            }
            else
            {
                _clockWidget = new QWClock();
                pnlRight.Controls.Add(_clockWidget);
                _clockWidget.Dock = DockStyle.Top;
            }
        }

        /// <summary>
        /// Ts the loket.
        /// </summary>
        private void TLoket()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(TLoket);
                Invoke(o);
            }
            else
            {
                var f = new LoketUI {MdiParent = this};
                f.Show();
            }
        }

        /// <summary>
        /// Ts the layanan.
        /// </summary>
        private void TLayanan()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(TLayanan);
                Invoke(o);
            }
            else
            {
                var f = new LayananUI {MdiParent = this};
                f.Show();
            }
        }

        /// <summary>
        /// Ts the keymap.
        /// </summary>
        private void TKeymap()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(TKeymap);
                Invoke(o);
            }
            else
            {
                var f = new KeymapUI {MdiParent = this};
                f.Show();
            }
        }

        private void MnuKeymapClick(object sender, EventArgs e)
        {
            var t = new Thread(TKeymap);
            t.Start();
        }

        /// <summary>
        /// Ts the keymap.
        /// </summary>
        private void TDatabase()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(TDatabase);
                Invoke(o);
            }
            else
            {
                var f = new DBLogonUI {MdiParent = this};
                f.Show();
            }
        }

        /// <summary>
        /// Ts the company.
        /// </summary>
        private void TCompany()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(TCompany);
                Invoke(o);
            }
            else
            {
                var f = new CompanyUI {MdiParent = this};
                f.Show();
            }
        }

        /// <summary>
        /// Ts the workflow.
        /// </summary>
        private void TWorkflow()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(TWorkflow);
                Invoke(o);
            }
            else
            {
                var f = new WorkflowUI {MdiParent = this};
                f.Show();
            }
        }

        /// <summary>
        /// Handles the Shown event of the MainUI control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void MainUI_Shown(object sender, EventArgs e)
        {
            var t = new Thread(TClock);
            t.Start();

            if (AuthenticateUser(this)) return;

            ApplicationExit();
        }

        /// <summary>
        /// Applications the exit.
        /// </summary>
        private void ApplicationExit()
        {
            Close();
            Dispose();
            Application.Exit();
        }

        /// <summary>
        /// Authenticates the user.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <returns></returns>
        public bool AuthenticateUser(IWin32Window parent)
        {
            DialogResult dlg;
            using (var f = new LogonPresentation())
            {
                dlg = f.ShowDialog(parent);
                _user = f.User;
            }

            if (dlg == DialogResult.OK)
                lblUser.Text = _user.UserId;

            return (dlg.Equals(DialogResult.OK));
        }

        /// <summary>
        /// Handles the Click event of the mnuExit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void mnuExit_Click(object sender, EventArgs e)
        {
            ApplicationExit();
        }

        /// <summary>
        /// Handles the Click event of the mnuLogoff control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void mnuLogoff_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }

        /// <summary>
        /// Handles the Click event of the mnuCascade control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void mnuCascade_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        /// <summary>
        /// Handles the Click event of the mnuTile control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void mnuTile_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        /// <summary>
        /// Handles the Click event of the mnuAbout control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void mnuAbout_Click(object sender, EventArgs e)
        {
            using (var f = new About())
                f.ShowDialog(this);
        }

        /// <summary>
        /// Handles the Click event of the mnuCompany control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void mnuCompany_Click(object sender, EventArgs e)
        {
            var t = new Thread(TCompany);
            t.Start();
        }

        /// <summary>
        /// Handles the Click event of the mnuDatabase control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void mnuDatabase_Click(object sender, EventArgs e)
        {
            var t = new Thread(TDatabase);
            t.Start();
        }

        /// <summary>
        /// Handles the Click event of the mnuWorkflow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void mnuWorkflow_Click(object sender, EventArgs e)
        {
            var t = new Thread(TWorkflow);
            t.Start();
        }

        /// <summary>
        /// Handles the Click event of the mnuLoket control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void mnuLoket_Click(object sender, EventArgs e)
        {
            var t = new Thread(TLoket);
            t.Start();
        }

        /// <summary>
        /// Handles the Click event of the mnuLayanan control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void mnuLayanan_Click(object sender, EventArgs e)
        {
            var t = new Thread(TLayanan);
            t.Start();
        }

        /// <summary>
        /// Handles the Resize event of the MainUI control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void MainUI_Resize(object sender, EventArgs e)
        {
            Refresh();
        }

        /// <summary>
        /// Handles the Click event of the tlbDatabase control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void tlbDatabase_Click(object sender, EventArgs e)
        {
            mnuDatabase_Click(this, null);
        }

        /// <summary>
        /// Handles the Click event of the tlbCompany control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void tlbCompany_Click(object sender, EventArgs e)
        {
            mnuCompany_Click(this, null);
        }

        /// <summary>
        /// Handles the Click event of the tlbWorkflow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void tlbWorkflow_Click(object sender, EventArgs e)
        {
            mnuWorkflow_Click(this, null);
        }

        /// <summary>
        /// Handles the Click event of the tlbKeymap control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void tlbKeymap_Click(object sender, EventArgs e)
        {
            MnuKeymapClick(this, null);
        }

        private void tlbLoket_Click(object sender, EventArgs e)
        {
            mnuLoket_Click(this, null);
        }

        /// <summary>
        /// Handles the Click event of the tlbLayanan control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void tlbLayanan_Click(object sender, EventArgs e)
        {
            mnuLayanan_Click(this, null);
        }

        private void tlbLogoff_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }

        /// <summary>
        /// Handles the Click event of the tlbExit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void tlbExit_Click(object sender, EventArgs e)
        {
            ApplicationExit();
        }

        /// <summary>
        /// Handles the Click event of the mnuPassword control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void mnuPassword_Click(object sender, EventArgs e)
        {
            using (var f = new PasswordUI(_user))
            {
                f.ShowDialog(this);
            }
        }
    }

    #region Nested type: DelegateThis

    /// <summary>
    /// DelegateThis
    /// </summary>
    internal delegate void DelegateThis();

    #endregion

    #region Nested type: DelegateTimer

    /// <summary>
    ///  DelegateTimer
    /// </summary>
    internal delegate void DelegateTimer(object stateInfo);

    #endregion
}